import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header


def connectServer():
    connect = smtplib.SMTP_SSL('smtp.qq.com', 465)
    connect.login("738897958@qq.com", 'lsagqrmxcvhrbajh')
    return connect


def sendEmail(sender, reciver, subject, content):
    connect = connectServer()
    msg = MIMEMultipart()
    msg['Subject'] = subject
    # 设置邮件发送者
    msg['From'] = sender
    # 设置邮件接受者
    msg['To'] = reciver
    # 添加⽂文字内容
    msg.attach(content)

    try:
        connect.sendmail(sender, reciver, msg.as_string())
        connect.quit()
        print("邮件发送成功")
    except smtplib.SMTPException as e:
        print('邮件发送失败')
        print(e)


if __name__ == '__main__':
    sender = "738897958@qq.com"
    reciver = "228985150@qq.com"
    subject = Header('找回密码', 'utf-8').encode()
    html = """
        <h2>请修改密码</h2>
        <p>密码已过期</p>
        <p>密码敏感操作</p>
        <img src='https://www.baidu.com/img/bd_logo1.png'> <center>百度图片</center>
<a href='https://www.baidu.com'>百度⼀下</a>
    """
    content = MIMEText(html, 'html', 'utf-8')
    sendEmail(sender, reciver, subject, content)
